package com.ibm.csorb.dao.impl;

import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.ibm.csorb.dao.CsTestcaseDao;
import com.ibm.csorb.entity.CsTestcase;

@Component
@Transactional
public class CsTestcaseDaoImpl extends HibernateDaoSupport implements
		CsTestcaseDao {
	private static final Log log = LogFactory.getLog(CsTestcaseDaoImpl.class);
	// property constants
	public static final String BUCKET_ID = "bucketId";
	public static final String TC_NAME = "tcName";
	public static final String TC_CLASSNAME = "tcClassname";
	public static final String DESCRIPTION = "description";
	public static final String PRE_TC_ID = "preTcId";
	public static final String SUC_TC_ID = "sucTcId";
	public static final String PRE_BUCKET_ID = "preBucketId";
	public static final String OTHER_PREREQUEST = "otherPrerequest";
	public static final String REMARK = "remark";
	public static final String TC_SEQUENCE = "tcSequence";

	protected void initDao() {
		// do nothing
	}

	public void save(CsTestcase transientInstance) {
		log.debug("saving CsTestcase instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(CsTestcase persistentInstance) {
		log.debug("deleting CsTestcase instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public CsTestcase findById(java.lang.Integer id) {
		log.debug("getting CsTestcase instance with id: " + id);
		try {
			CsTestcase instance = (CsTestcase) getHibernateTemplate().get(
					"com.ibm.csorb.entity.CsTestcase", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	@SuppressWarnings("unchecked")
	public List<CsTestcase> findByExample(CsTestcase instance) {
		log.debug("finding CsTestcase instance by example");
		try {
			List<CsTestcase> results = getHibernateTemplate().findByExample(
					instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	@SuppressWarnings("unchecked")
	public List<CsTestcase> findByProperty(String propertyName, Object value) {
		log.debug("finding CsTestcase instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from CsTestcase as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List<CsTestcase> findByBucketId(Object bucketId) {
		return findByProperty(BUCKET_ID, bucketId);
	}

	public List<CsTestcase> findByTcName(Object tcName) {
		return findByProperty(TC_NAME, tcName);
	}

	public List<CsTestcase> findByTcClassname(Object tcClassname) {
		return findByProperty(TC_CLASSNAME, tcClassname);
	}

	public List<CsTestcase> findByDescription(Object description) {
		return findByProperty(DESCRIPTION, description);
	}

	public List<CsTestcase> findByPreTcId(Object preTcId) {
		return findByProperty(PRE_TC_ID, preTcId);
	}

	public List<CsTestcase> findBySucTcId(Object sucTcId) {
		return findByProperty(SUC_TC_ID, sucTcId);
	}

	public List<CsTestcase> findByPreBucketId(Object preBucketId) {
		return findByProperty(PRE_BUCKET_ID, preBucketId);
	}

	public List<CsTestcase> findByOtherPrerequest(Object otherPrerequest) {
		return findByProperty(OTHER_PREREQUEST, otherPrerequest);
	}

	public List<CsTestcase> findByRemark(Object remark) {
		return findByProperty(REMARK, remark);
	}

	public List<CsTestcase> findByTcSequence(Object tcSequence) {
		return findByProperty(TC_SEQUENCE, tcSequence);
	}

	@SuppressWarnings("unchecked")
	public List<CsTestcase> findAll() {
		log.debug("finding all CsTestcase instances");
		try {
			String queryString = "from CsTestcase";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public CsTestcase merge(CsTestcase detachedInstance) {
		log.debug("merging CsTestcase instance");
		try {
			CsTestcase result = (CsTestcase) getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(CsTestcase instance) {
		log.debug("attaching dirty CsTestcase instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(CsTestcase instance) {
		log.debug("attaching clean CsTestcase instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static CsTestcaseDaoImpl getFromApplicationContext(
			ApplicationContext ctx) {
		return (CsTestcaseDaoImpl) ctx.getBean("CsTestcaseDAO");
	}
}